import java.util.ArrayList;
import java.awt.*;
public class Tower {

public final static int WINHEIGHT = 100;
public final static int DECEPTION_DECAY_RATE = 2;
private ArrayList<Block> blocks;
 
	public Tower()
	{
		blocks = new ArrayList<Block>();
	}
	
	public Tower(ArrayList<Block> blocks)
	{
		this.blocks = blocks;
	}
	
	
	public int getHeight()
	{
		int count = 0;
		
		for(Block b: blocks)
		{
			count += b.getHeight();
		}
		
		return count;
	}
	
	
	public void addBlock(Block b)
	{
		blocks.add(b);
	}
	
	
	public void removeBlock(Block b)
	{
		blocks.remove(b);
	}
	
	public Block getTopBlock(){
		return blocks.get(blocks.size()-1);
	}
	
	public Block getBottomBlock(){
		return blocks.get(0);
	}
	
	public void turnTowerCheck(){
		//check for dead blocks
		for(int i=0; i<blocks.size();i++){
			Block currentBlock= blocks.get(i);
			if (currentBlock.getHealth()<=0 || currentBlock.getHeight()<=0){
				blocks.remove(i);
			}
			else if(currentBlock.getType()==3){
				int prevBlockType= 5;
				int nextBlockType= 6;
				try{
					Block prevBlock = blocks.get(i-1);
					prevBlockType=prevBlock.getType();
				}
				catch(IndexOutOfBoundsException e){
					
				}
				try{
					Block nextBlock = blocks.get(i+1);
					nextBlockType=nextBlock.getType();
				}
				catch(IndexOutOfBoundsException e){
					
				}
				if(prevBlockType==nextBlockType){
					System.out.println("CONVERSION");
					currentBlock.setType(prevBlockType);
				}
				else{
					currentBlock.takeDamage(DECEPTION_DECAY_RATE);
					if (currentBlock.getHealth()==0)
						blocks.remove(i);
				}
				
			}
		}
		
		
	}
	
	
	public Block getBlock(int index)
	{
		if (index < 0)
		{
			index = 0;
		}
		if (index > blocks.size())
		{
			index = blocks.size();
		}
		return blocks.get(index);
	}
	
	
	public int getNumBlocks()
	{
		return blocks.size();
	}
	
	public boolean checkWin()
	{
		return (getHeight() > 99);
	}
	
	public boolean checkLose()
	{
		return (blocks.size() == 0);
	}
	
	public void drawTower(Graphics g, int x, int ybase)
	{
		Block b; int height = 0;
		for(int i = 0; i < blocks.size(); i++)
		{
			b = blocks.get(i);
			ybase -= b.getHeight()*4;
			height += b.getHeight();
			switch (b.getType())
			{
				default: case 0: 
				g.setColor(new Color(0,0,Math.min(255,64+ 3*b.getHealth()))); break;
				
				case 1:
				g.setColor(new Color(Math.min(255,96+ 3*b.getHealth()),Math.min(255,96+ 3*b.getHealth()),0)); break;
				
				case 2:
				g.setColor(new Color(Math.min(128+4*b.getHealth(),255),50,50)); break;
				
				case 3:
				g.setColor(new Color(Math.min(255,4*b.getHealth()),Math.min(255,4*b.getHealth()),Math.min(255,4*b.getHealth()))); break;
			}
			g.fillRect(x, ybase, 100, b.getHeight()*4);
			g.setColor(Color.white);
			g.drawString("HP: "+ b.getHealth(), x, ybase+10);
			g.drawString("Height: "+ b.getHeight(), x+40, ybase+10);
			g.setColor(Color.black);
			g.drawString(""+ height, x+100, ybase+6);
		}
	}
	
	public String toString()
	{
		return("Tower: height=" + getHeight() + " numBlocks=" + blocks.size());
	}

}
